import React, { Component } from 'react';

export default class App extends Component {
  state = {
    currentUrl: '/home',
  };
  componentDidMount() {
    console.log('window.location----->', window.location);
    window.addEventListener('hashchange', () => {
      this.setState({
        currentUrl: window.location.hash.slice(1),
      });
    });
  }

  render() {
    return (
      <div>
        <ul>
          <li>
            <a href="#/home">首页</a>
          </li>
          <li>
            <a href="#/myMusic">我的音乐</a>
          </li>
          <li>
            <a href="#/myFriend">我的朋友</a>
          </li>
        </ul>

        {this.state.currentUrl === '/home' && <Home></Home>}
        {this.state.currentUrl === '/myMusic' && <MyMusic></MyMusic>}
        {this.state.currentUrl === '/myFriend' && <MyFriend></MyFriend>}
      </div>
    );
  }
}

function Home() {
  return <h3>我是首页组件</h3>;
}
function MyMusic() {
  return <h3>我是我的音乐组件</h3>;
}
function MyFriend() {
  return <h3>我是我的朋友组件</h3>;
}
